package com.seewo.dubbo.debugger.admin.utils;

import com.alibaba.fastjson.JSON;
import com.seewo.dubbo.debugger.api.model.ModelDef;
import com.seewo.dubbo.debugger.api.model.ParamDef;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author chenliangliang
 * @date 2018/8/7
 */
public class CommonUtil {

    private CommonUtil() {
    }

    public static String parse2ResponseJson(List<ParamDef> propertyList) {
        StringBuilder sb = new StringBuilder("{");
        for (ParamDef paramDef : propertyList) {
            String name = paramDef.getName();
            if (StringUtils.equals(name, "void")) {
                continue;
            }
            sb.append("\"").append(name).append("\"").append(":");
            ModelDef modelDef = paramDef.getModelDef();
            if (modelDef == null) {
                String type = paramDef.getType();
                switch (type) {
                    case "integer":
                        sb.append(1);
                        break;
                    case "long":
                        sb.append(6005319759269438692L);
                        break;
                    case "string":
                        String description = paramDef.getDescription();
                        sb.append("\"");
                        if (StringUtils.isEmpty(description)) {
                            sb.append(name);
                        } else {
                            sb.append(description);
                        }
                        sb.append("\"");
                        break;
                    case "boolean":
                        sb.append(true);
                        break;
                    case "float":
                        sb.append(6.8F);
                        break;
                    case "double":
                        sb.append(8.88D);
                        break;
                    case "array":
                        sb.append("[]");
                        break;
                    default:

                        break;
                }
            } else {
                sb.append(parse2ResponseJson(modelDef.getPropertyList()));
            }
            sb.append(",");
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("}");
        return sb.toString();
    }


    public static String parse2RequestJson(List<ParamDef> propertyList) {
        StringBuilder sb = new StringBuilder("{");
        for (ParamDef paramDef : propertyList) {
            sb.append("\"").append(paramDef.getName()).append("\"").append(":");
            ModelDef modelDef = paramDef.getModelDef();
            if (modelDef == null) {
                sb.append("\"\"");
            } else {
                sb.append(parse2RequestJson(modelDef.getPropertyList()));
            }
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("}");
        return sb.toString();
    }


    public static String formatJson(String json) {
        try {
            return JSON.toJSONString(JSON.parseObject(json), true);
        } catch (Exception e) {
            return json;
        }
    }

}
